﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SwiftMVVM.ViewModel;
using System.ComponentModel;

namespace MineSweeper.Data
{
    //implementing DependentBindingObject for listbox in Statistics window
    public class DifficultyManager : BindingObject
    {
        // Creates a collection that can be read by the statistic window's listboxes
        readonly ObservableCollection<Difficulty> difficultyList = new ObservableCollection<Difficulty>();

        readonly Difficulty beginner;
        readonly Difficulty intermediate;
        readonly Difficulty advanced;

        /// <summary>
        /// Manager class that contains an ObservableList of Difficulty objects
        /// </summary>
        public DifficultyManager()
        {
            //Constructor: int roundsPlayed, int roundsWon, int winStreak, int loseStreak, int currentStreak, decimal winPercent, string difName
            beginner = MineSweeper.Data.Difficulty.Create(this, Properties.Settings.Default.StatGamesPlayedBeg, Properties.Settings.Default.StatGamesWonBeg, 
                + Properties.Settings.Default.StatWinStreakBeg, Properties.Settings.Default.StatLossStreakBeg, Properties.Settings.Default.StatCurrentStreakBeg,
                + Properties.Settings.Default.StatWinPercentBeg, "Beginner", Properties.Settings.Default.StatBestTimesBeg);
            intermediate = MineSweeper.Data.Difficulty.Create(this, Properties.Settings.Default.StatGamesPlayedInt, Properties.Settings.Default.StatGamesWonInt,
                + Properties.Settings.Default.StatWinStreakInt, Properties.Settings.Default.StatLossStreakInt, Properties.Settings.Default.StatCurrentStreakInt,
                + Properties.Settings.Default.StatWinPercentInt, "Intermediate", Properties.Settings.Default.StatBestTimesInt);
            advanced = MineSweeper.Data.Difficulty.Create(this, Properties.Settings.Default.StatGamesPlayedAdv, Properties.Settings.Default.StatGamesWonAdv, 
                + Properties.Settings.Default.StatWinStreakAdv, Properties.Settings.Default.StatLossStreakAdv, Properties.Settings.Default.StatCurrentStreakAdv, 
                + Properties.Settings.Default.StatWinPercentAdv, "Advanced", Properties.Settings.Default.StatBestTimesAdv);

            difficultyList.Add(beginner);
            difficultyList.Add(intermediate);
            difficultyList.Add(advanced);
        }

        /// <summary>
        /// Sets all settings to properties of Difficulty
        /// </summary>
        public void Save()
        {
            int count1 = 0;
            int count2 = 0;
            int count3 = 0;

            Properties.Settings.Default.StatGamesPlayedBeg = beginner.Played;
            Properties.Settings.Default.StatGamesWonBeg = beginner.Wins;
            Properties.Settings.Default.StatCurrentStreakBeg = beginner.Current;
            Properties.Settings.Default.StatWinPercentBeg = beginner.WinPercent;
            Properties.Settings.Default.StatWinStreakBeg = beginner.WinStreak;
            Properties.Settings.Default.StatLossStreakBeg = beginner.LossStreak;
            beginner.Times.Clear();
            foreach (int i in Properties.Settings.Default.StatBestTimesBeg)
            {
                beginner.Times.Add(i);
                count1++;
            }

            Properties.Settings.Default.StatGamesPlayedInt = intermediate.Played;
            Properties.Settings.Default.StatGamesWonInt = intermediate.Wins;
            Properties.Settings.Default.StatCurrentStreakInt = intermediate.Current;
            Properties.Settings.Default.StatWinPercentInt = intermediate.WinPercent;
            Properties.Settings.Default.StatWinStreakInt = intermediate.WinStreak;
            Properties.Settings.Default.StatLossStreakInt = intermediate.LossStreak;
            intermediate.Times.Clear();
            foreach (int i in Properties.Settings.Default.StatBestTimesInt)
            {
                intermediate.Times.Add(i);
                count2++;
            }

            Properties.Settings.Default.StatGamesPlayedAdv = advanced.Played;
            Properties.Settings.Default.StatGamesWonAdv = advanced.Wins;
            Properties.Settings.Default.StatCurrentStreakAdv = advanced.Current;
            Properties.Settings.Default.StatWinPercentAdv = advanced.WinPercent;
            Properties.Settings.Default.StatWinStreakAdv = advanced.WinStreak;
            Properties.Settings.Default.StatLossStreakAdv = advanced.LossStreak;
            advanced.Times.Clear();
            foreach (int i in Properties.Settings.Default.StatBestTimesAdv)
            {
                advanced.Times.Add(i);
                count3++;
            }
        }

        /// <summary>
        /// ObservableList of Difficulty objects
        /// </summary>
        public ObservableCollection<Difficulty> DifficultyList
        {
            get { return this.difficultyList; }
        }
    }
}
